﻿Imports System.Data.OleDb

Public Class PlayerList
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddButtonIcon(phSearchButton, btnSearch, "glyphicon glyphicon-search")

        If Not IsPostBack Then
            Dim clubID As String = Request.Params("ClubID")

            If Not String.IsNullOrEmpty(clubID) Then

                ddlClub.Visible = False

                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
                oleDbConn.Open()

                Dim selectClubNameSQL As String = "SELECT ClubName FROM Clubs WHERE ID=@f1"
                Dim selectClubNameCmd As OleDbCommand = New OleDbCommand(selectClubNameSQL, oleDbConn)
                selectClubNameCmd.CommandType = CommandType.Text
                selectClubNameCmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
                Dim selectClubNameDr As String = selectClubNameCmd.ExecuteScalar()

                If Not String.IsNullOrEmpty(selectClubNameDr) Then
                    btnBackClub.Visible = True
                    gvClubPlayerList.Visible = True
                    gvFullPlayerList.Visible = False
                    lblClubName.Text = ": " & selectClubNameDr

                    If (gvClubPlayerList.Rows.Count = 0) Then
                        phSearch.Visible = False
                        gvClubPlayerList.Visible = False

                        pnlUserMessage_Error.Visible = True
                        lblUserMessage_Error.Text = "No players were added to this club yet"
                    Else
                        pnlUserMessage_Error.Visible = False
                    End If

                    selectClubNameCmd.Dispose()
                Else
                    phSearch.Visible = False
                    gvClubPlayerList.Visible = False
                    Session("UserMessage_Error") = "The specified club does not exist!"
                    Response.Redirect("~/ClubList.aspx")
                End If

                oleDbConn.Close()
                oleDbConn.Dispose()
            Else
                gvClubPlayerList.Visible = False
                btnBackClub.Visible = False

                If (gvFullPlayerList.Rows.Count = 0) Then
                    gvFullPlayerList.Visible = False
                    pnlUserMessage_Error.Visible = True
                    lblUserMessage_Error.Text = "No players were added yet"
                Else
                    gvFullPlayerList.Visible = True
                    pnlUserMessage_Error.Visible = False
                End If
            End If
        End If
    End Sub

    Protected Sub btnAddNewPlayer_Click(sender As Object, e As EventArgs) Handles btnAddNewPlayer.Click
        If Not String.IsNullOrEmpty(Request.Params("ClubID")) Then
            Response.Redirect("~/AddPlayer.aspx?ClubID=" & Request.Params("ClubID"))
        Else
            Response.Redirect("~/AddPlayer.aspx")
        End If
    End Sub

    Protected Sub btnBackClub_Click(sender As Object, e As EventArgs) Handles btnBackClub.Click
        Response.Redirect("~/ClubDetail.aspx?ClubID=" & Request.Params("ClubID"))
    End Sub

    Protected Sub btnRSSFeed_Click(sender As Object, e As EventArgs) Handles btnRSSFeed.Click
        If Not String.IsNullOrEmpty(Request.Params("ClubID")) Then
            Response.Redirect("~/Feeds/PlayerListFeed.aspx?ClubID=" & Request.Params("ClubID"))
        Else
            Response.Redirect("~/Feeds/PlayerListFeed.aspx")
        End If

    End Sub

    Private Sub AddButtonIcon(ph As PlaceHolder, btn As Button, IconClass As String, Optional CustomLabelStyle As String = "")

        If btn.Visible = False Then Exit Sub
        Dim g As New HtmlGenericControl
        g.ID = "labelFor_" + btn.ID
        g.TagName = "label"
        g.Attributes.Add("for", btn.ClientID)
        g.Attributes.Add("class", "" + btn.CssClass + "")
        If Not CustomLabelStyle = "" Then g.Attributes.Add("style", CustomLabelStyle)
        g.InnerHtml = "<i class=""" + IconClass + """></i> " + btn.Text
        ph.Controls.Add(g)
        btn.Attributes.Add("style", "display:none;")

    End Sub

End Class